﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using TotalCrytography.Data;
using TotalCrytography.Protector;

namespace TotalCrytography
{
    public partial class LoginGUI : Form
    {
        private UserDTO m_loginUser;

        public UserDTO LoginUser
        {
            get { return m_loginUser; }
            set { m_loginUser = value; }
        }

        private string m_loginPass;

        public string LoginPassword
        {
            get { return m_loginPass; }
            set { m_loginPass = value; }
        }

        public LoginGUI()
        {
            InitializeComponent();
            m_loginUser = null;
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            m_loginUser = UserBUS.GetUser(tbEmail.Text);
            if (m_loginUser != null)
            {
                if (Hash_Algorithm.VerifyHashWithSalt(tbPass.Text, "SHA1", m_loginUser.HashPass))
                {
                    MainGUI.SetCurrentUser(m_loginUser);
                    MainGUI.SetCurrentPass(tbPass.Text);
                    MessageBox.Show("Login successfully!", "TotalCrytography", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
                    MainGUI.isLogin = true;
                    DialogResult = DialogResult.OK;
                    Close();
                    return;
                }
            }
            MessageBox.Show("Email or password is not correct!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
            MainGUI.isLogin = false;
        }

        private void tbEmail_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                btnLogin_Click(sender, e);
        }

        private void tbPass_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                btnLogin_Click(sender, e);
        }

    }
}
